//
//  CZStatusBottomView.swift
//  GZWeibo666
//
//  Created by Apple on 15/11/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
//转发和评论的代理
protocol CZStatusBottomViewDelegate:NSObjectProtocol{
//    转发
   func  forwardButtonClick(status: CZStatus)
//    评论
   func  commentButtonClick(status: CZStatus)
}

class CZStatusBottomView: UIView {
    
    
//    有一个闭包
    // 定义闭包

    
//    有一个代理属性
    weak var delegate: CZStatusBottomViewDelegate?
    
    /// 微博模型
    var status: CZStatus? {
        didSet {
            setButton(forwardButton, count: status!.reposts_count)
            setButton(commentButton, count: status!.comments_count)
            setButton(likeButton, count: status!.attitudes_count)
            
            //            setButton(forwardButton, count: 8888)
            //            setButton(commentButton, count: 10000)
            //            setButton(lickButton, count: 12000)
        }
    }
    // MARK: - 设置按钮文字
    private func setButton(button: UIButton, count: Int) {
        if count >= 10000 {
            let final = CGFloat(count) / 10000.0
            var titleText = String(format: "%.1f万", arguments: [final])
            titleText = titleText.stringByReplacingOccurrencesOfString(".0", withString: "")
            
            button.setTitle(titleText, forState: UIControlState.Normal)
        } else if count > 0 {
            button.setTitle("\(count)", forState: UIControlState.Normal)
        }
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    // MARK: - 准备UI
    private func prepareUI() {
        backgroundColor = UIColor(white: 0.98, alpha: 1)
        
        
        // 添加子控件
        addSubview(forwardButton)
        forwardButton.addTarget(self, action: "fwButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        addSubview(commentButton)
        commentButton.addTarget(self, action: "cmButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        addSubview(likeButton)
        addSubview(sImageViewFirst)
        addSubview(sImageViewSecond)
        
        // 添加约束
        /// 平铺子控件
        /// self: 父控件
        /// views: 哪些子控件要平铺
        /// insets边距
        self.ff_HorizontalTile([forwardButton, commentButton, likeButton], insets: UIEdgeInsetsZero)
        
        sImageViewFirst.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: forwardButton, size: nil)
        
        sImageViewSecond.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: commentButton, size: nil)
    }
    
    //    转发按钮的点击事件
    func  fwButtonClick(){
        print("点击了转发按钮")
//        调用代理方法
//        self.delegate?.forwardButtonClick()
        self.delegate?.forwardButtonClick(self.status!)
        
        
    }
    
    
    func   cmButtonClick(){
        print("点击了评论按钮")
        //        调用代理方法
      
        
        self.delegate?.commentButtonClick(self.status!)
    }
    
    // MARK: - 懒加载
    /// 转发按钮
    private lazy var forwardButton: UIButton = UIButton(imageName: "timeline_icon_retweet", title: "转发")
    
 

    
//    private lazy var forwardButton: UIButton = {
//        let button = UIButton()
//        
//        // 按钮图片
//        button.setImage(UIImage(named: "timeline_icon_retweet"), forState: UIControlState.Normal)
//        
//        // 按钮文字
//        button.setTitle("转发", forState: UIControlState.Normal)
//        
//        // 文字颜色
//        button.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
//        
//        // 文字大小
//        button.titleLabel?.font = UIFont.systemFontOfSize(12)
//        
//        return button
//    }()
    
    /// 评论按钮
    private lazy var commentButton: UIButton = UIButton(imageName: "timeline_icon_comment", title: "评论")
//    private lazy var commentButton: UIButton = {
//        let button = UIButton()
//        
//        // 按钮图片
//        button.setImage(UIImage(named: "timeline_icon_comment"), forState: UIControlState.Normal)
//        
//        // 按钮文字
//        button.setTitle("评论", forState: UIControlState.Normal)
//        
//        // 文字颜色
//        button.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
//        
//        // 文字大小
//        button.titleLabel?.font = UIFont.systemFontOfSize(12)
//        
//        return button
//    }()
    
    /// 赞按钮
    private lazy var likeButton: UIButton = UIButton(imageName: "timeline_icon_unlike", title: "赞")
//    private lazy var likeButton: UIButton = {
//        let button = UIButton()
//        
//        // 按钮图片
//        button.setImage(UIImage(named: "timeline_icon_unlike"), forState: UIControlState.Normal)
//        
//        // 按钮文字
//        button.setTitle("赞", forState: UIControlState.Normal)
//        
//        // 文字颜色
//        button.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
//        
//        // 文字大小
//        button.titleLabel?.font = UIFont.systemFontOfSize(12)
//        
//        return button
//    }()
    
    /// 分割线1
    private lazy var sImageViewFirst: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line_highlighted"))
    
    /// 分割线2
    private lazy var sImageViewSecond: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line_highlighted"))
}
